<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2018 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2018-12-04
 * Time: 15:27
 */

namespace App\Library\Mall;


use App\Library\AuthenticatedUser;
use App\Models\Item;
use Illuminate\Http\Request;

trait CartManagers
{
    use AuthenticatedUser;

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $quantity = $request->input('quantity', 1);
        if ($itemid && $quantity) {
            $item = $this->user()->cartItems()->where('itemid', $itemid)->first();
            if ($item) {
                $item->increment('quantity', $quantity);
            } else {
                $item = Item::find($itemid);
                $this->user()->cartItems()->create([
                    'itemid' => $item->itemid,
                    'shop_id' => $item->shop_id,
                    'title' => $item->title,
                    'quantity' => $quantity,
                    'price' => $item->price,
                    'thumb' => $item->thumb,
                    'image' => $item->image,
                    'sku_id' => 0,
                    'sku_name' => '',
                ]);
            }
        }
        return ajaxReturn();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateQuantity(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $quantity = $request->input('quantity', 1);
        if ($itemid && $quantity) {
            $this->user()->cartItems()->where('itemid', $itemid)->update(['quantity' => $quantity]);
        }
        return ajaxReturn();
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {

        $items = $request->post('items', []);
        if ($items) {
            $this->user()->cartItems()->whereIn('itemid', $items)->delete();
        }
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getItems(Request $request)
    {
        $itemids = $request->input('itemids', []);
        if (empty($itemids)) {
            $carts = $this->user()->cartItems()->with(['item', 'shop'])->get();
        } else {
            $carts = $this->user()->cartItems()->with(['item', 'shop'])->whereIn('itemid', $itemids)->get();
        }

        $items = [];
        foreach ($carts as $cart) {
            if ($cart->item && $cart->shop) {
                if (!isset($items[$cart->shop_id])) {
                    $cart->shop->logo = image_url($cart->shop->logo);
                    $items[$cart->shop_id] = [
                        'shop' => $cart->shop,
                        'items' => []
                    ];
                }

                $item = $cart->item;
                $item->quantity = $cart->quantity;
                $item->cart_price = $cart->price;
                $item->sku_id = $cart->sku_id;
                $item->sku_name = $cart->sku_name;
                $items[$cart->shop_id]['items'][] = $item;
            } else {
                $cart->delete();
            }
        }

        return ajaxReturn(['items' => array_values($items)]);
    }
}
